package com.masterclient.app.modules.firstpager.fragments.service;

import android.support.annotation.NonNull;

import com.eoner.baselibrary.bean.firstpager.AwesomeDetailBean;
import com.eoner.baselibrary.bean.firstpager.GoodProduct;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.base.MessageCallBack;
import com.masterclient.app.httpservice.RetrofitUtil;


import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：sean
 * 创建时间：2017/6/9 14:48
 * 功能说明：首页推荐Service
 */

public class FirstPagerService {

    /**
     * 首页好物列表
     * @param page 页数
     * @param callBack
     */
    public void getNewGoodProducts(String page,String page_size,MessageCallBack<GoodProduct.DataBean> callBack){
        RetrofitUtil.getInstance().getNewGoodProducts(NetEnvManager.BASE_SURL+"1/good_product/get_list/"+page+"-"+page_size+".html")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GoodProduct>() {

            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }

            @Override
            public void onNext(@io.reactivex.annotations.NonNull GoodProduct goodProduct) {
                if("0".equals(goodProduct.getCode())){
                    callBack.onSuccess(goodProduct.getData());
                }
            }

            @Override
            public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        });
    }

  public void getAwesomeDetail(@NonNull String id,MessageCallBack<AwesomeDetailBean.DataBean> callBack){

        RetrofitUtil.getInstance().getAwesomeDetail(id).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<AwesomeDetailBean>(){

            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }

            @Override
            public void onNext(AwesomeDetailBean awesomeDetailBean) {
                if(awesomeDetailBean!=null && awesomeDetailBean.getData()!=null){
                    callBack.onSuccess(awesomeDetailBean.getData());
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                callBack.onError(e.toString());
            }

            @Override
            public void onComplete() {
            }
        });
    }

}
